package com.heima.article.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.article.service.ApArticleService;
import com.heima.model.article.dtos.ArticleHomeDto;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.common.dtos.ResponseResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * ApArticleService
 *
 * @version 1.0
 * @project heima-leadnews
 */
@Service
public class ApArticleServiceImpl extends ServiceImpl<ApArticleMapper, ApArticle> implements ApArticleService {


    private static final String ALL = "__all__";
    private static final String DYNA = "__dyna__";

    @Override
    public List<ApArticle> load(ArticleHomeDto articleHomeDto) {
        Page<ApArticle> pageQuery = new Page<>(1,articleHomeDto.getSize(),false);
        ApArticle apArticleQuery = new ApArticle();
        if (!StringUtils.equalsAny(articleHomeDto.getTag(), ALL, DYNA)){
            apArticleQuery.setChannelId(Integer.valueOf(articleHomeDto.getTag()));
        }
        Page<ApArticle> page = baseMapper.selectPageLimit(pageQuery,apArticleQuery);
        return page.getRecords();
    }
}
